package com.electron.power.aio.mod.cha;

import java.nio.ByteBuffer;
import java.time.LocalDateTime;

import org.smartboot.socket.Protocol;
import org.smartboot.socket.transport.AioSession;
import org.springframework.stereotype.Component;

import com.electron.power.common.Coding;
import com.electron.power.model.mod.ModRoot;
import com.electron.power.model.mod.ModStr;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class ModCharProtocol implements Protocol<ModStr> {

	@Override
	public ModStr decode(ByteBuffer byteBuffer, AioSession<ModStr> session) {
		try {
			//使用char数据格式传输byte,数据扩大了1倍.
			Integer frameLenth = ModRoot.getFrameLenth();
			log.debug("frame length is :{}",frameLenth);
			if (byteBuffer.remaining() < frameLenth) {
				return null;
			}
			byte[] dst = new byte[frameLenth];
			byteBuffer.get(dst, 0, frameLenth);
			String testDst = "";
			for(int j=0;j<dst.length;j++) {
				testDst = testDst+dst[j]+",";
			}
			log.info("接收到的原始数据是:{}",testDst);
			String result = Coding.byte2hex(dst,true);
			//开始校验CRC,如果校验失败.断开连接.
			boolean passValidated = ModRoot.validateCRC(result);
			if(!passValidated) {
				log.warn("校验失败,断开连接,错误数据{}",result);
				session.close();
				return null;
			}
			
			ModStr modStr = new ModStr();
			modStr.setData(result);
			modStr.setCreatTime(LocalDateTime.now());
			modStr.setLastTime(LocalDateTime.now());
			return modStr;
		} catch (Exception e) {
			log.error("错误", e);
			return null;
		}
	}

//	public ByteBuffer encode(ModStr msg, AioSession<ModStr> session) {
//		try {
//			int frameLength = Mod.getFrameLenth()*2;
//			ByteBuffer byteBuffer = ByteBuffer.allocate(frameLength);
//			// ByteBuffer byteBuffer = ByteBuffer.allocate(500);
//			String str = "550300661A2B3C4D5E8000000000000000000000000000000000000003E70000000000000CE40000138800000000000000000000000000000000000003E70000000000000CFD0000138800000000000000000000000000000000000003E70000000000000C99000013887252";
//			byte[] hex2byte = Coding.hex2byte(str);
//			byteBuffer.put(hex2byte);
//			// b.putInt(s);
////			byteBuffer.rewind();
//			byteBuffer.flip();
//			return byteBuffer;
//		} catch (Exception e) {
//			log.error("写数据错误", e);
//			return null;
//		}
//	}
	
	public static void main(String[] args) {
		try {
			String str = "550300661A2B3C4D5E800000000000000000000000000000000A000003E70000000900000DF7000013880000000000000000000000000000000A000003E70000000900000DDE000013880000000000000000000000000000000A000003E70000000900000D610000138840D7";
			System.out.println(str.length());
			byte[] hex2byte = Coding.hex2byte(str.getBytes());
			int frameLength = ModRoot.getFrameLenth();
			ByteBuffer byteBuffer = ByteBuffer.allocate(frameLength);
			byteBuffer.put(hex2byte);
			byteBuffer.flip();
			byte[] dst = new byte[frameLength];
			byteBuffer.get(dst, 0, frameLength);
			String result = Coding.byte2hex(dst,false);
			System.out.println(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}
